<template src="./index.html"></template>

<script>
import MedicalRecordFamilyMedicalHistoryService from "../../../../../../_services/medicalRecordFamilyMedicalHistory.service";
export default {
  name: "TianDingPatientMeFamilyHistoryModifyComponent",
  data() {
    return {
      params: {
        FamilyID: "0", //家族病史编号
        UserID: "0", //病患编号
        FamilyType: "0", //家族类型 = ['0', '1']
        Relation: "", //关系
        Medical: "", //病史
        OtherMedical: "", //其它病史
        Remark: "" //备注
      },
      showRelation: false,
      RelationList: [
        {
          key: 0,
          value: "男方本人",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 1,
          value: "女方本人",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 2,
          value: "母亲",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 3,
          value: "父亲",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 4,
          value: "祖母",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 5,
          value: "祖父",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 6,
          value: "外祖母",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 7,
          value: "外祖父",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 8,
          value: "弟弟",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 9,
          value: "哥哥",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 10,
          value: "妹妹",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 11,
          value: "姐姐",
          isRadio: false,
          check: false,
          show: true
        }
      ],
      showMedical: false,
      MedicalList: [
        {
          key: 0,
          value: "高血压",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 1,
          value: "糖尿病",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 2,
          value: "血栓",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 3,
          value: "风湿病",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 4,
          value: "心脑血管疾病",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 5,
          value: "无",
          isRadio: true,
          check: false,
          show: true
        },
        {
          key: 6,
          value: "其它",
          isRadio: false,
          check: false,
          show: true
        }
      ]
    };
  },
  mounted() {
    let familyHistory = JSON.parse(this.local$.getItem("tdFamilyHistory"));
    if (familyHistory !== null) {
      this.params = familyHistory;
      this.utils$.dataFormat(this.params);
    }
    this.getFamilyType(this.params.FamilyType);
  },
  methods: {
    getFamilyType(event) {
      this.RelationList.filter((v, i, a) => {
        if (event === "0") {
          if (v.key === 0) {
            v.show = true;
          }
          if (v.key === 1) {
            v.show = false;
          }
        } else if (event === "1") {
          if (v.key === 0) {
            v.show = false;
          }
          if (v.key === 1) {
            v.show = true;
          }
        }
        return a;
      });
    },
    changeRelation(event) {
      if (this.showRelation) {
        this.showRelation = false;
      } else {
        this.showRelation = true;
      }
    },
    changeMedical(event) {
      if (this.showMedical) {
        this.showMedical = false;
      } else {
        this.showMedical = true;
      }
    },
    getRelation(event) {
      console.log(event);
      let temp = [];
      event.forEach(value => {
        let item = this.RelationList.find(i => i.key === value);
        temp.push(item.value);
      });
      this.params.Relation = temp.toString();
    },
    getMedical(event) {
      let temp = [];
      if (event[0] === 5) {
        this.showMedical = false;
        let item = this.MedicalList.find(i => i.key === event[0]);
        temp.push(item.value);
      } else {
        event.forEach(value => {
          let item = this.MedicalList.find(i => i.key === value);
          temp.push(item.value);
        });
      }
      this.params.Medical = temp.toString();
    },
    btnSubmit() {
      if (!this.params.Relation) {
        this.toast$("请选择关系");
        return;
      }

      if (!this.params.Medical) {
        this.toast$("请选择病史");
        return;
      }

      if (this.params.Medical.split(",").includes("其它")) {
        if (!this.params.OtherMedical) {
          this.toast$("请填写其它病种");
          return;
        }
      }

      let user = JSON.parse(this.local$.getItem("tdUser"));

      let params = {
        FamilyID: parseInt(this.params.FamilyID), //家族病史编号
        UserID: user.UserID, //病患编号
        FamilyType: parseInt(this.params.FamilyType), //家族类型 = ['0', '1']
        Relation: this.params.Relation, //关系
        Medical: this.params.Medical, //病史
        OtherMedical: this.params.OtherMedical, //其它病史
        Remark: this.params.Remark //备注
      };

      if (params.FamilyID > 0) {
        MedicalRecordFamilyMedicalHistoryService.updateFamilyMedicalHistory(
          params
        ).then(response => {
          if (response && response.Data) {
            this.toast$("保存成功");
          } else {
            this.toast$("保存失败");
          }
          this.$router.push({ name: "tdPatientMeMedicalFamilyHistory" });
        });
      } else {
        MedicalRecordFamilyMedicalHistoryService.addFamilyMedicalHistory(
          params
        ).then(response => {
          if (response && response.Data) {
            this.toast$("保存成功");
          } else {
            this.toast$("保存失败");
          }
          this.$router.push({ name: "tdPatientMeMedicalFamilyHistory" });
        });
      }
    }
  }
};
</script>
